package com.android.killtencent.activity;

import android.support.v4.app.Fragment;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.android.killtencent.BaseApp;
import com.android.killtencent.R;
import com.android.killtencent.connection.XmppConnectionHelper;
import com.android.killtencent.utils.AppManager;
import com.android.killtencent.utils.FragmentTabUtils;
import com.android.pc.ioc.inject.InjectInit;
import com.android.pc.ioc.inject.InjectLayer;
import com.android.pc.ioc.inject.InjectView;

import java.util.ArrayList;

@InjectLayer(R.layout.activity_main)
public class MainActivity extends BaseActivity implements FragmentTabUtils.OnRgsExtraCheckedChangedListener {
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    @InjectView(R.id.main_rgs)
    private RadioGroup rgs;
    private long firstTime = 0;
    private FragmentTabUtils tabUtils;

    @InjectInit
    private void init() {
        tabUtils = new FragmentTabUtils(getSupportFragmentManager(), fragments, R.id.fragment_container, rgs);
        tabUtils.setOnRgsExtraCheckedChangedListener(this);
    }

    @Override
    public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {

    }

    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) { //如果两次按键时间间隔大于2秒，则不退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;//更新firstTime
            return;
        } else {
            //两次按键小于2秒时，退出应用
            AppManager.getAppManager().AppExit(BaseApp.getInstance());
            XmppConnectionHelper.getInstance().closeConnection();
        }
    }
}